package bean;

import java.beans.Transient;
import java.io.File;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

import luceneutil.V100LunceneWorker;

import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;

import applicationutil.ApplicationConstant;
import applicationutil.ApplicationUtil;
import entities.KeySeacher;
import entities.V100;
import entities.VnScoreDoc;

@ManagedBean(name = "resultBean")
@ViewScoped
public class ResultBean implements Serializable {
	private transient List<VnScoreDoc> listScoreDocs = new ArrayList<VnScoreDoc>();
	private String textSearch;
	private List<V100> resultList;
	private int numberResult;
	private long timeDuration;
	// paing
	private int offset = 0;
	private int limit = 10;
	private int totalrow; // tong so dong
	private int totalpage; // tong so trang
	private int current; // traung hien tai
	private int startpage = 1; // page bat dau
	private int endpage = 1; // page ket thuc
	private int mode = 1;// 1: search binh thuong, 2 search chinh xác
	private String webSiteSearch;//tim theo website
	private String notText;//tu khong muon tim thay
	@PostConstruct
	public void init() {
		System.out.println("---vao postcustruct result");
		// paging
		int a;
		Object ppage = ApplicationUtil.getRequest().getParameter("p");
		if (ppage != null) {
			try {
				a = Integer.parseInt(ppage.toString());
			} catch (Exception ex) {
				a = 1;
			}
			if (a == 1) {
				setCurrent(1);
				setOffset(0);
			} else {
				setCurrent(a);
				setOffset((a - 1) * limit);
			}

		} else {
			setCurrent(1);
			setOffset(0);
		}
		loadListResult();
	}

	public void loadListResult() {
		listScoreDocs = (List<VnScoreDoc>) ApplicationUtil
				.getSession(ApplicationConstant.SCORE_DOCS);
		V100LunceneWorker worker = new V100LunceneWorker(
				ApplicationConstant.INDEX_PATH);
		resultList = worker.getResultList(offset, limit, listScoreDocs);

		// paging
		int t = listScoreDocs.size();
		setTotalrow(t);
	}

	public void doSearch() {
		System.out.println("---do search resultbean");
		V100LunceneWorker worker = new V100LunceneWorker(
				ApplicationConstant.INDEX_PATH);
		try {
			KeySeacher keySeacher = new KeySeacher(textSearch, mode,
					webSiteSearch, notText);
			List<VnScoreDoc> listScoreDocs = worker.searchAll(keySeacher);
			/*
			 * System.out .println(
			 * "-----------------------in diem ra thu----------------------------------"
			 * ); Directory directory; IndexSearcher indexSearcher = null; try {
			 * directory = FSDirectory.open(new File(
			 * ApplicationConstant.INDEX_PATH)); IndexReader indexReader =
			 * DirectoryReader.open(directory); indexSearcher = new
			 * IndexSearcher(indexReader); } catch (Exception ex) {
			 * ex.printStackTrace(); } for (VnScoreDoc s : listScoreDocs) {
			 * System.out
			 * .println("----------------------------------------------");
			 * System.out.println("---doc:" + s.getScoreDoc().doc);
			 * 
			 * org.apache.lucene.document.Document document = indexSearcher
			 * .doc(s.getScoreDoc().doc); System.out.println("---pv100:" +
			 * document.get(ApplicationConstant.MYPAGE_FIELD_ID));
			 * 
			 * System.out.println("---score:" + s.getScoreDoc().score);
			 * System.out
			 * .println("----------------------------------------------"); }
			 */
			ApplicationUtil.setSession(ApplicationConstant.SCORE_DOCS,
					listScoreDocs);
			ApplicationUtil.setSession(ApplicationConstant.TEXT_SEARCH,
					textSearch);
			offset=0;
			current=1;
			loadListResult();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public List<V100> getResultList() {
		return resultList;
	}

	public void setResultList(List<V100> resultList) {
		this.resultList = resultList;
	}

	public int getNumberResult() {
		if (ApplicationUtil.getSession(ApplicationConstant.NUM_RESULT) != null) {
			numberResult = Integer.parseInt(ApplicationUtil.getSession(
					ApplicationConstant.NUM_RESULT).toString());
			return numberResult;
		}
		return 0;
	}

	public void setNumberResult(int numberResult) {
		this.numberResult = numberResult;
	}

	public double getTimeDuration() {
		long endTime=System.currentTimeMillis();
		if (ApplicationUtil.getSession(ApplicationConstant.TIME_DURATION) != null) {
			long beginTime = Long.parseLong(ApplicationUtil.getSession(
					ApplicationConstant.TIME_DURATION).toString());
			timeDuration=endTime-beginTime;
			return (double) timeDuration / 1000;
		}
		return 0;
	}

	public void setTimeDuration(long timeDuration) {
		this.timeDuration = timeDuration;
	}

	public int getOffset() {
		return offset;
	}

	public void setOffset(int offset) {
		this.offset = offset;
	}

	public int getLimit() {
		return limit;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}

	public int getTotalrow() {
		return totalrow;
	}

	public void setTotalrow(int totalrow) {
		totalpage = totalrow / limit;
		System.out.println("---tong so trang:" + totalpage);
		if (totalrow % limit > 0)
			totalpage++;
		if (getTotalpage() <= 10) {
			setStartpage(1);
			setEndpage(getTotalpage());
		} else {
			setStartpage(current - 5);
			setEndpage(current + 5);
		}
		this.totalrow = totalrow;
	}

	public int getTotalpage() {
		return totalpage;
	}

	public void setTotalpage(int totalpage) {
		this.totalpage = totalpage;
	}

	public int getCurrent() {
		return current;
	}

	public void setCurrent(int current) {
		this.current = current;
	}

	public int getStartpage() {
		if (startpage < 1) {
			startpage = 1;
		}
		return startpage;
	}

	public void setStartpage(int startpage) {
		this.startpage = startpage;
	}

	public int getEndpage() {
		if (endpage > totalpage) {
			endpage = totalpage;
		}
		return endpage;
	}

	public void setEndpage(int endpage) {
		this.endpage = endpage;
	}

	public String getTextSearch() {
		textSearch = ApplicationUtil
				.getSession(ApplicationConstant.TEXT_SEARCH).toString();
		return textSearch;
	}

	public void setTextSearch(String textSearch) {
		this.textSearch = textSearch;
	}

	public int getMode() {
		return mode;
	}

	public void setMode(int mode) {
		this.mode = mode;
	}

	public String getWebSiteSearch() {
		return webSiteSearch;
	}

	public void setWebSiteSearch(String webSiteSearch) {
		this.webSiteSearch = webSiteSearch;
	}

	public List<VnScoreDoc> getListScoreDocs() {
		return listScoreDocs;
	}

	public void setListScoreDocs(List<VnScoreDoc> listScoreDocs) {
		this.listScoreDocs = listScoreDocs;
	}

	public String getNotText() {
		return notText;
	}

	public void setNotText(String notText) {
		this.notText = notText;
	}
	
}
